<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:bil="http://java.sun.com/jsf/composite/bill"
      xmlns:phe="http://java.sun.com/jsf/composite/pharmacy/inward"
      xmlns:phi="http://java.sun.com/jsf/composite/pharmacy"
      xmlns:bill="http://java.sun.com/jsf/composite/inward">

    <h:body>

        <ui:composition template="/resources/template/template.xhtml">

            <ui:define name="content">


                <h:form id="bill" >

                    <p:commandButton id="nullButton2" value="No Action" action="#" style="display: none;" onclick="onSubmitButton();"  ></p:commandButton>
                    <p:defaultCommand  target="btnAdd" />  



                    <p:panel rendered="#{!pharmacySaleBhtController.billPreview}" style="width: fit-content;" >



                        <h:panelGrid id="sale" columns="1" class="alignTop" style="width: fit-content;">
                            <p:panel style="width: fit-content;">
                                <f:facet name="header" >
                                    <h:outputLabel value="BHT ISSUE " />
                                </f:facet>

                                <p:panel>


                                    <f:facet name="header" >
                                        <h:panelGrid columns="20"  >
                                            <p:commandButton value="Settle" ajax="false"
                                                             action="#{pharmacySaleBhtController.settlePharmacyBhtIssue()}" 
                                                             actionListener="#{pharmacySaleBhtController.calculateAllRates}"  >

                                            </p:commandButton>
                                            <p:commandButton accesskey="n" value="New Pharmacy Bill" ajax="false"
                                                             action="pharmacy_bill_issue_bht"
                                                             actionListener="#{pharmacySaleBhtController.resetAll}"  ></p:commandButton>


                                        </h:panelGrid>


                                    </f:facet>
                                    <h:panelGroup id="panelError" >
                                        <h:panelGroup  
                                            rendered="#{pharmacySaleBhtController.errorMessage ne null}"
                                            style="background-color: yellow; color: red; display: block; margin: 2px; border: 1px solid red; padding: 3px; width: 95%;">
                                            <p:outputLabel value="#{pharmacySaleBhtController.errorMessage}" ></p:outputLabel>
                                        </h:panelGroup>
                                    </h:panelGroup>





                                    <p:panel header="Patient Detail">                                        
                                        <h:outputLabel value="Type Patinet Name or BHT : "/>
                                        <p:autoComplete widgetVar="aPt2"  id="acPt2" forceSelection="true" converter="admis"
                                                        value="#{pharmacySaleBhtController.patientEncounter}" 
                                                        completeMethod="#{admissionController.completePatient}"
                                                        var="apt2" itemLabel="#{apt2.bhtNo}"
                                                        itemValue="#{apt2}" size="30"  style="width: 400px;">

                                            <p:column >
                                                <h:outputLabel value="#{apt2.bhtNo}"/> 
                                            </p:column>
                                            <p:column >
                                                <h:outputLabel value="#{apt2.patient.person.name}"/> 
                                            </p:column>
                                            <p:column>
                                                <h:outputLabel value="#{apt2.currentPatientRoom.roomFacilityCharge.name}"/>
                                            </p:column>
                                            <p:ajax event="itemSelect" process="acPt2" update="#{p:component('gpDetail')}" />
                                        </p:autoComplete>  

                                    </p:panel>

                                    <p:panel id="gpDetail">
                                        <bill:bhtDetail admission="#{pharmacySaleBhtController.patientEncounter}"/>
                                    </p:panel>

                                </p:panel>



                                <h:panelGrid columns="1" >
                                    <h:panelGrid columns="6" >
                                        <h:panelGrid columns="1" >
                                            <p:outputLabel value="Medicines/Devices" ></p:outputLabel>

                                            <style>
                                                .ui-autocomplete-input {width:350px!important;display: block;}
                                                .ui-autocomplete{display: block!important;}
                                            </style>
                                            <p:autoComplete accesskey="i"   forceSelection="true"  id="acStock" 
                                                            value="#{pharmacySaleBhtController.stock}" 
                                                            converter="stockCon" 
                                                            completeMethod="#{pharmacySaleController.completeAvailableStocks}" 
                                                            var="i" itemLabel="#{i.itemBatch.item.name}" itemValue="#{i}" 
                                                            >
                                                <p:column headerText="Item">
                                                    <h:outputLabel value="#{i.itemBatch.item.name}" 
                                                                   style="#{commonController.currentDateTime > i.itemBatch.dateOfExpire ?'color: red; ': 
                                                                            commonController.dateAfterThreeMonthsCurrentDateTime > i.itemBatch.dateOfExpire ?'color: red; background-color: yellow;':''}"></h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Code">
                                                    <h:outputLabel value="#{i.itemBatch.item.code}" ></h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Stocks">
                                                    <h:outputLabel value="#{i.stock}" >
                                                        <f:convertNumber pattern="#,###" ></f:convertNumber>
                                                    </h:outputLabel>
                                                </p:column>
                                                <p:column headerText="Expiry">
                                                    <h:outputLabel value="#{i.itemBatch.dateOfExpire}" style="#{commonController.currentDateTime > i.itemBatch.dateOfExpire ?'color: red; ': 
                                                                            commonController.dateAfterThreeMonthsCurrentDateTime > i.itemBatch.dateOfExpire ?'color: red; background-color: yellow;':''}" >
                                                        <f:convertDateTime pattern="dd MMMM yyyy" ></f:convertDateTime>
                                                    </h:outputLabel>
                                                </p:column>
                                                <p:ajax event="focus" process="acStock" ></p:ajax>
                                                <p:ajax event="itemSelect"   
                                                        listener="#{pharmacySaleBhtController.handleSelect}"  
                                                        update="focusQty" ></p:ajax>
                                            </p:autoComplete>

                                        </h:panelGrid>

                                        <h:panelGrid columns="1" >
                                            <p:outputLabel value="Quantity" ></p:outputLabel>
                                            <p:inputText autocomplete="off"  accesskey="q"   id="txtQty" value="#{pharmacySaleBhtController.qty}" style="width: 50px!important;"   >
                                                <p:ajax event="keyup"  listener="#{pharmacySaleBhtController.calculateBillItemListner}" process="acStock txtQty"></p:ajax>
                                                <p:ajax event="blur"  listener="#{pharmacySaleBhtController.calculateBillItemListner}" process="acStock txtQty"></p:ajax>
                                            </p:inputText>
                                        </h:panelGrid>

                                        <p:commandButton  accesskey="a" id="btnAdd" value="Add" 
                                                          action="#{pharmacySaleBhtController.addBillItem}" process="@this acStock txtQty" update=":#{p:component('tblBillItem')} acStock focusItem :#{p:component('panelError')} " onclick="onSubmitButton();"  ></p:commandButton>

                                        <p:focus id="focusQty" for="txtQty" ></p:focus>
                                        <p:focus id="focusItem" for="acStock" ></p:focus>

                                    </h:panelGrid>

                                    <p:panel header="Bill Items" id="pBis">
                                        <p:dataTable id="tblBillItem" value="#{pharmacySaleBhtController.preBill.billItems}"
                                                     var="bi" rowIndexVar="s" editable="true" sortBy="#{bi.searialNo}" >

                                            <p:ajax event="rowEdit" listener="#{pharmacySaleBhtController.onEdit}"/>  
                                            <p:ajax event="rowEditCancel" listener="#{pharmacySaleBhtController.onEdit}"/> 


                                            <p:column headerText="Item">
                                                <h:outputLabel value="#{bi.pharmaceuticalBillItem.itemBatch.item.name}" ></h:outputLabel>
                                            </p:column>
                                            <p:column headerText="Quantity">
                                                <p:cellEditor>  
                                                    <f:facet name="output">  
                                                        <h:outputText value="#{bi.qty}" >
                                                            <f:convertNumber integerOnly="true" />
                                                        </h:outputText>
                                                    </f:facet>  
                                                    <f:facet name="input">  
                                                        <p:inputText autocomplete="off" id="ipTblQty" value="#{bi.qty}" style="width:96%"/>  
                                                    </f:facet>  
                                                </p:cellEditor> 
                                            </p:column>

                                            <p:column style="width:6%">  
                                                <p:rowEditor />  
                                            </p:column>  



                                            <p:column headerText="Expiry">
                                                <h:outputLabel value="#{bi.pharmaceuticalBillItem.itemBatch.dateOfExpire}" >
                                                    <f:convertDateTime pattern="dd MM yyyy" ></f:convertDateTime>
                                                </h:outputLabel>

                                            </p:column>
                                            <p:column headerText="Remove">
                                                <p:commandButton value="Remove" action="#{pharmacySaleBhtController.removeBillItem(bi)}" ajax="false" onclick="onSubmitButton();"  >

                                                </p:commandButton>
                                            </p:column>

                                        </p:dataTable>
                                    </p:panel>

                                </h:panelGrid>

                            </p:panel>





                        </h:panelGrid>
                    </p:panel>


                </h:form>

                <h:form>


                    <p:panel  rendered="#{pharmacySaleBhtController.billPreview}" >




                        <p:commandButton id="nullButton3" value="No Action" action="#" style="display: none;" onclick="onSubmitButton();"  ></p:commandButton>
                        <p:defaultCommand  target="btnPrint" />  

                        <div class="nonPrintBlock" >
                            <p:commandButton accesskey="p" id="btnPrint" value="Print" ajax="false" action="#" >
                                <p:printer target="gpBillPreview" ></p:printer>
                            </p:commandButton>
                            <p:commandButton accesskey="n" value="New Pharmacy Bill" ajax="false"
                                             action="pharmacy_bill_issue_bht"
                                             actionListener="#{pharmacySaleBhtController.resetAll()}" onclick="onSubmitButton();"  ></p:commandButton>

                        </div>

                        <!--                        <div >
                                                    <h:panelGroup   id="gpBillPreview"   > 
                                                        <phe:issueBill bill="#{pharmacySaleBhtController.printBill}"/>
                                                    </h:panelGroup>
                                                </div>-->

                        <h:panelGroup   id="gpBillPreview" > 

                            <h:panelGroup   id="gpBillPreviewSingle" >



                            </h:panelGroup>

                            <h:panelGroup id="gpBillPreviewDouble" rendered="#{sessionController.currentPreference.pharmacyBillPaperType eq 'PosPaper'}"> 

                                <div >
                                    <h:panelGroup     rendered="#{sessionController.currentPreference.pharmacyBillPrabodha eq true}"> 
                                        <phi:saleBill_prabodha bill="#{pharmacySaleBhtController.printBill}"></phi:saleBill_prabodha>
                                    </h:panelGroup>
                                </div>

                                <div >
                                    <h:panelGroup rendered="#{sessionController.currentPreference.pharmacyBillPrabodha eq false}" > 
                                        <phe:issueBill bill="#{pharmacySaleBhtController.printBill}"/>
                                    </h:panelGroup>
                                </div>

                            </h:panelGroup>

                            <h:panelGroup id="gpBillPreviewFiveFive" rendered="#{sessionController.currentPreference.pharmacyBillPaperType eq 'FiveFivePaper'}"> 

                                <div >
                                    <h:panelGroup rendered="#{sessionController.currentPreference.pharmacyBillPrabodha eq false}" > 
                                        <phi:saleBill_five_five bill="#{pharmacySaleBhtController.printBill}"></phi:saleBill_five_five>
                                    </h:panelGroup>
                                </div>                                
                            </h:panelGroup>                            

                        </h:panelGroup>



                    </p:panel>



                </h:form>

            </ui:define>

        </ui:composition>

    </h:body>
</html>
